﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpotsHub.RouteService.Strategy
{
    public interface IStrategy
    {
        void Init(PlanType plan, IRouteSerivce service, Dictionary<string, object> settings);
        void Caculate();
        int Priorty { get; }
    }

    public abstract class StrategyBase : IStrategy
    {
        protected IRouteSerivce _service;
        protected Dictionary<string, object> _settings;
        protected PlanType Plan;
        public abstract void Caculate();

        public virtual void Init(PlanType plan, IRouteSerivce service, Dictionary<string, object> settings)
        {
            _service = service;
            _settings = settings;

            if (plan.WareHouse == null)
            {
                throw new ArgumentNullException("Plan.WareHouse");
            }

            if (plan.DriverAssignments == null || plan.DriverAssignments.Length <= 0)
            {
                throw new ArgumentException("Drivers are not provided.", "Plan.DriverAssignments");
            }

            this.Plan = plan;
        }

        public abstract int Priorty { get; }

        protected void Log(string format, params object[] args)
        {
            if (null != _service && null != _service.Logger)
            {
                _service.Logger.Log(format, args);
            }
        }

        protected object GetSetting(string name)
        {
            if (null == _settings || !_settings.ContainsKey(name))
            {
                throw new InvalidOperationException(
                    String.Format("The current planning strategy ({0}) needs setting entry [{1}].", this, name));
            }

            return _settings[name];
        }
    }
}
